package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //购物项添加操作
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //将用户id和创建时间添加到shoppingCart对象中
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //查看数据库中是否存在购物项信息
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, shoppingCart.getUserId());
        if (shoppingCart.getDishId() == null) {//如果菜品id为null那么存储的就是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {//else里代表存储的是菜品
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }//做sql分支拼接

        //查询出数据库中是否存在购物项
        ShoppingCart one = shoppingCartService.getOne(wrapper);

        if (one == null) {//判断数据库中是否存在购物项信息 没有做添加
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
        } else {//else存在 做 +1 更新操作
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
            shoppingCart = one;
        }

        return R.success(shoppingCart);
    }

    //购物车展示
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        //首先获取到当前登录的用户id
        Long userId = BaseContext.getCurrentId();
        //根据当前登录的用户的id查询出所有购物项
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //sql语句拼接
        wrapper.eq(ShoppingCart::getUserId, userId);
        //执行查询
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        //响应页面
        return R.success(list);
    }

    //清空用户购物项
    @DeleteMapping("/clean")
    public R<String> clean() {
        //获取到当前登录用户的id
        Long userId = BaseContext.getCurrentId();
        //条件删除
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //拼接sql语句
        wrapper.eq(ShoppingCart::getUserId, userId);
        //执行删除操作
        shoppingCartService.remove(wrapper);
        return R.success("删除成功");
    }

    //购物项-1操作
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //获取当前用户id备用
        Long userId = BaseContext.getCurrentId();
        //先查查此次传进来的是菜还是套
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //拼接where user_id = ? 条件
        wrapper.eq(ShoppingCart::getUserId, userId);

        if (shoppingCart.getDishId() == null) {//==null传进来的就是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {//否则就是菜
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        //获取到的菜品或套餐的信息
        ShoppingCart one = shoppingCartService.getOne(wrapper);

        one.setNumber(one.getNumber() - 1);
        //判断-1后是否为0
        if (one.getNumber() == 0) {//如果-1为0直接删除该条购物项信息
            shoppingCartService.remove(wrapper);
        } else {//如果-1后不为0则更新数据
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }

}
